package com.crazy.bookstore.exception;

import com.crazy.bookstore.common.ApiRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: 卢辉宇
 * @date: 2022/12/27 14:36
 * @description:
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*通过LoggerFactory得到log来进行在控制台的打印显示*/
    static final Logger log =  LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //捕获的是系统异常
    @ExceptionHandler(Exception.class)
    public ApiRestResponse handlerException(Exception e){
        log.error("Default Exception",e);
        return ApiRestResponse.error(ExceptionEnum.SYSTEM_ERROR);
    }

    /*在service层的异常向上抛出时，返回给前端的提示封装 成一致的json对象*/
    //捕获的自定义异常
    @ExceptionHandler(MyException.class)
    public ApiRestResponse handlerMallException(MyException e){
        log.error("MyException",e);
        return ApiRestResponse.error(e.getCODE(),e.getMSG());
    }
}
